package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanmait.schedule.entity.CompanyUser;
import com.wanmait.schedule.entity.Userinfo;
import com.wanmait.schedule.mapper.CompanyUserMapper;
import com.wanmait.schedule.service.CompanyUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.schedule.service.DepartmentService;
import com.wanmait.schedule.service.PositionService;
import com.wanmait.schedule.service.UserinfoService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 企业用户表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-03-02
 */
@Service
public class CompanyUserServiceImpl extends ServiceImpl<CompanyUserMapper, CompanyUser> implements CompanyUserService {
@Resource
private UserinfoService userinfoService;
    @Resource
    private PositionService positionService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private CompanyUserMapper companyUserMapper;
    //查询所有企业用户相对应的职位
    @Override
    public List<CompanyUser> findCompanyUser(Integer companyId) {
        LambdaQueryWrapper<CompanyUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CompanyUser::getCompanyId,companyId);
        lambdaQueryWrapper.isNotNull(CompanyUser::getDepartmentId);
        List<CompanyUser> companyUsers=this.list(lambdaQueryWrapper);
        companyUsers.forEach(companyUser -> {
            companyUser.setUserinfo(userinfoService.getById(companyUser.getUserinfoId()));
            companyUser.setPosition(positionService.getById(companyUser.getPositionId()));
            companyUser.setDepartment(departmentService.getById(companyUser.getDepartmentId()));
        });
        return companyUsers;
    }

    @Override
    public CompanyUser isJoinedCompany(Integer userinfoId) {
        LambdaQueryWrapper<CompanyUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyUser::getUserinfoId,userinfoId);
        CompanyUser companyUser =companyUserMapper.selectOne(queryWrapper);
        if(companyUser != null){
            return companyUser;
        }
        return null;
    }

    @Override
    public CompanyUser getCompanyUserByUserinfoId(Integer id) {
        LambdaQueryWrapper<CompanyUser>queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(CompanyUser::getUserinfoId,id);
        CompanyUser companyUser = new CompanyUser();
        companyUser = this.getOne(queryWrapper);
        return companyUser;
    }

    @Override
    public List<CompanyUser> selectCompanyUsersOfDep(Integer companyId, Integer departmentId) {
        LambdaQueryWrapper<CompanyUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyUser::getCompanyId,companyId);
        queryWrapper.eq(CompanyUser::getDepartmentId,departmentId);
        queryWrapper.and(wrapper->{
            wrapper.eq(CompanyUser::getPositionId,3).or().eq(CompanyUser::getPositionId,2);
        });
        List<CompanyUser> companyUsers = this.list(queryWrapper);
        companyUsers.forEach(companyUser ->
                companyUser.setUserinfo(userinfoService.getById(companyUser.getUserinfoId()))
        );
        return companyUsers;
    }
}
